home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12154 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  71 lines

  1. Path: ix.netcom.com!netnews
  2. From: jlilley@ix.netcom.com (John Lilley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie requires help!
  5. Date: 18 Mar 1996 17:18:05 GMT
  6. Organization: Netcom
  7. Distribution: world
  8. Message-ID: <4ik5sd$14k@reader2.ix.netcom.com>
  9. References: <AMeOMBA0erSxEwc0@waichung.demon.co.uk>
  10. NNTP-Posting-Host: den-co8-14.ix.netcom.com
  11. Mime-Version: 1.0
  12. Content-Type: Text/Plain; charset=US-ASCII
  13. X-NETCOM-Date: Mon Mar 18  9:18:05 AM PST 1996
  14. X-Newsreader: WinVN 0.99.7
  15.  
  16. In article <AMeOMBA0erSxEwc0@waichung.demon.co.uk>, kyn@waichung.demon.co.uk says...
  17. >
  18. >I require help in constructing a binary search tree in c++, as i am 
  19. >relatively new to the language i am not sure how to go about
  20. >implementing it. 
  21. > Do i construct a class called btree or do i use the following:-
  22. >
  23. > struct node 
  24. >  {
  25. >   data  d;
  26. >   struct node  *left;
  27. >   struct node  *right;
  28. >  }
  29.  
  30. Well, the answer for this would be too much like an entire C++ book, so
  31. first -- go buy some C++ books, like:
  32.     Lippmann "C++ Primer"
  33.     Meyers "Effective C++"
  34.     Cargill "C++ Programming Style"
  35.  
  36. The above notwithstanding, you want to make the procedures in your pascal
  37. program members of your "Node" class, assuming that "Node" corresponds
  38. to what you are calling "Subtree" in Pascal.  I'm also adding some code
  39. for inialization and such, that you'll have to dig around for in a C++ book:
  40.  
  41. class data {/* whatever is in data */};
  42.  
  43. class Node
  44. {
  45. public:
  46.    Node(const data& d_) d(d_) {}
  47.    void SearchAndInsert(const data& newDatum)
  48.    {
  49.        // Presumes that class data holds both key and data.
  50.        // Note that there is no "subtree" argument because the
  51.        // method implicitly operates on the "Node" object for 
  52.        // which is was invoked.
  53.    }
  54.    void PrintTree();
  55. private:
  56.    data  d;
  57.    Node  *left;
  58.    Node  *right;
  59. }
  60.  
  61. void BuildTree()
  62. {
  63.     // This is a function because it is not an operation on Node
  64. }
  65.  
  66.  
  67. Its at least a start...
  68.  
  69. john lilley
  70.  
  71.